<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no, viewport-fit=cover" />
    <title>资质审查</title>
    <link rel="stylesheet" href="../css/vant.css">
    <link rel="stylesheet" href="./css/infoList2.css">
    <style>
        .uploadImg>.van-image__img,
        .uploadImg>.van-uploader__preview-image {
            width: 1.8667rem;
            height: 1.8667rem;
        }
    </style>
</head>

<body>
    <div id="app" v-cloak>
        <div class="topBox">
            <van-nav-bar title="资质" left-text="返回" left-arrow @click-left="onClickLeft"></van-nav-bar>
            <!-- 步骤条 -->
            <div class="stepsBox">
                <van-steps :active="active">
                    <van-step>企业资料</van-step>
                    <van-step>产品类型</van-step>
                    <van-step>产品资料</van-step>
                </van-steps>
            </div>
        </div>


        <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
            <div class="mainBox">
                <!-- 企业资料 -->
                <template v-if="active==0">
                    <div class="saveBtn imgBox">
                        <van-button type="info" @click="addCom" size="small" v-if="active==0">添加企业</van-button>
                    </div>
                    <template v-if="companyList.length!=0">
                        <div class="imgBox" v-for="(item,index) in companyList" :key="index">
                            <div class="title">企业资料
                                <span @click="removeCom(index)" v-if="item.companyId==''">移除</span>
                            </div>
                            <van-field v-model="item.companyName" label="企业名称" placeholder="请输入企业名称" input-align="right"
                                @focus="comFocusHandle(index)" @blur="comBlurHandle(index)"></van-field>
                            <div class="imgList" v-if="item.companyImgList.length!=0">
                                <div v-for="(imgitem,imgIndex) in item.companyImgList">
                                    <img :src="path+'qualFile/'+imgitem" alt=""
                                        @click="checkImg( item.companyImgList,1,imgIndex)">
                                    <div class="delIcon" v-if="item.state!=1"><van-icon name="cross"
                                            @click.stop="delComImg(companyList,index,imgIndex)" />
                                    </div>
                                </div>
                            </div>
                            <div class="uploader" v-else>
                                <div class="uplader-img" v-if="item.uploadImgList.length==0">
                                    <van-icon color="#666" name="photo-o" size="30px" />
                                </div>
                                <van-uploader v-model="item.uploadImgList" multiple :max-count="1"
                                    :after-read="editComHandle(companyList[index],2)">
                                    <div class="uplader">营业执照</div>
                                </van-uploader>
                            </div>
                        </div>
                    </template>
                    <div v-else class="emptyBox">
                        <img src="./img/zanwu.png" alt="">
                        暂无企业信息
                    </div>
                </template>

                <!-- 产品类型 -->
                <template v-if="active==1">
                    <div v-for="(item,index) in proTypeList" :key="index">
                        <div class="imgBox">
                            <van-field v-model="item.companyName" label="企业" placeholder="请选择企业" input-align="right"
                                readonly right-icon="arrow-down" @click="showHandle(index,'company')" />
                        </div>
                        <div class="imgBox">
                            <van-field v-model="item.typeName" label="产品类型" placeholder="请选择产品类型" input-align="right"
                                readonly right-icon="arrow-down" @click="showHandle(index,'type')" />
                        </div>
                        <div class="imgBox" v-if="item.companyId!=''&&item.typeName!=''&&item.qualId!=0">
                            <div class="applyBox">
                                <van-tag type="primary" v-if="item.status==0">审核中</van-tag>
                                <van-tag type="success" v-else-if="item.status==1">审核通过</van-tag>
                                <van-tag type="danger" v-else-if="item.status==2">审核驳回</van-tag>
                                <span>
                                    <template v-if="item.status==0">
                                        请耐心等待
                                    </template>
                                    <template v-if="item.status==1&&item.feedback">
                                        备注：{{item.feedback}}
                                    </template>
                                    <template v-if="item.status==2">
                                        驳回原因：{{item.feedback}}
                                    </template>
                                </span>
                            </div>
                        </div>
                        <div class="imgBox" v-for="(ele,eIndex) in item.ziZhiList" :key="index">
                            <div class="title">{{ele.comQualName}} </div>
                            <div class="imgList" v-if="ele.imgList.length!=0">
                                <div v-for="(imgItem,imgIndex) in ele.imgList">
                                    <template v-if="imgItem.type!='pdf'&&imgItem.type!='doc'">
                                        <img :src="path+'qualFile/'+imgItem.img" alt=""
                                            @click="checkImg(ele.imgList,2,imgIndex)">
                                        <div class="delIcon" v-if="item.status!=1"><van-icon name="cross"
                                                @click.stop="delImg(item.ziZhiList,eIndex,imgIndex)" />
                                    </template>
                                    <template v-else>
                                        <div class="fileBox">
                                            <van-button @click="showFile(imgItem.img,imgItem.type)"
                                                size="mini">{{imgItem.type=='pdf'?(isIos?'预览PDF':'下载PDF'):'预览文档'}}</van-button>
                                            <div class="delIcon" v-if="item.status!=1"><van-icon name="cross"
                                                    @click.stop="delImg(item.ziZhiList,eIndex,imgIndex)" />
                                            </div>
                                        </div>
                                    </template>
                                </div>
                            </div>

                            <div class="uploader" @click="typeId=ele.qualProductId;ziZhiId=ele.comQualId"
                                v-if="item.status!=1">
                                <div class="uplader-img" v-if="ele.uploadImgList.length==0">
                                    <van-icon color="#666" name="photo-o" size="30px" />
                                </div>
                                <van-uploader accept="*" v-model="ele.uploadImgList" multiple :max-count="9"
                                    :before-read='beforeRead'>
                                    <div class="uplader">上传文件</div>
                                </van-uploader>
                            </div>
                        </div>
                    </div>
                </template>


                <!-- 产品资料 -->
                <template v-if="active==2">
                    <div>
                        <div class="imgBox">
                            <van-field v-model="proInfo.companyName" label="企业" placeholder="请选择企业" input-align="right"
                                readonly />
                        </div>
                        <div class="imgBox">
                            <van-field v-model="proInfo.typeName" label="产品类型" placeholder="请选择产品类型" input-align="right"
                                readonly />
                        </div>
                        <div class="imgBox">
                            <van-field v-model="proInfo.productName" label="产品" placeholder="若新增产品，请直接输入产品名称"
                                input-align="right" right-icon="arrow-down"
                                @click-right-icon="showHandle(0,'product')" />
                        </div>
                        <div class="imgBox" v-if="proInfo.productName!=''&&proInfo.qualId!=0&& proInfo.isHaveZiZhi">
                            <div class="applyBox">
                                <van-tag type="primary" v-if="proInfo.status==0">审核中</van-tag>
                                <van-tag type="success" v-else-if="proInfo.status==1">审核通过</van-tag>
                                <van-tag type="danger" v-else-if="proInfo.status==2">审核驳回</van-tag>
                                <span>
                                    <template v-if="proInfo.status==0">
                                        请耐心等待
                                    </template>
                                    <template v-if="proInfo.status==1&&proInfo.feedback">
                                        备注：{{proInfo.feedback}}
                                    </template>
                                    <template v-if="proInfo.status==2">
                                        驳回原因：{{proInfo.feedback}}
                                    </template>
                                </span>
                            </div>
                        </div>
                        <div class="imgBox" v-for="(ele,eIndex) in proInfo.ziZhiList" :key="eIndex">
                            <div class="title">{{ele.proQualName}}</div>
                            <div class="imgList" v-if="ele.imgList.length!=0">
                                <div v-for="(imgItem,imgIndex) in ele.imgList">
                                    <template v-if="imgItem.type!='pdf'&&imgItem.type!='doc'">
                                        <img :src="path+'qualFile/'+imgItem.img" alt=""
                                            @click="checkImg(ele.imgList,2,imgIndex)">
                                        <div class="delIcon" v-if="proInfo.status!=1"><van-icon name="cross"
                                                @click.stop="delImg(proInfo.ziZhiList,eIndex,imgIndex)" />
                                    </template>
                                    <template v-else>
                                        <div class="fileBox">
                                            <van-button @click="showFile(imgItem.img,imgItem.type)"
                                                size="mini">{{imgItem.type=='pdf'?(isIos?'预览PDF':'下载PDF'):'预览文档'}}</van-button>
                                            <div class="delIcon" v-if="proInfo.status!=1"><van-icon name="cross"
                                                    @click.stop="delImg(proInfo.ziZhiList,eIndex,imgIndex)" />
                                            </div>
                                        </div>
                                    </template>
                                </div>
                            </div>

                            <div class="uploader" @click="typeId=proInfo.typeId;ziZhiId=ele.proQualId"
                                v-if="proInfo.status!=1">
                                <div class="uplader-img" v-if="ele.uploadImgList.length==0">
                                    <van-icon color="#666" name="photo-o" size="30px" />
                                </div>
                                <van-uploader accept="*" v-model="ele.uploadImgList" multiple :max-count="9"
                                    :before-read='beforeRead'>
                                    <div class="uplader">上传文件</div>
                                </van-uploader>
                            </div>
                            <div class="applyStatus">
                                <van-tag type="primary" v-if="ele.status==0">审核中</van-tag>
                                <van-tag type="success" v-else-if="ele.status==1">通过</van-tag>
                                <van-tag type="danger" v-else-if="ele.status==2">驳回</van-tag>
                            </div>
                            <div class="applyBox" v-if="ele.status==2">驳回原因：{{ele.passNote}}</div>
                        </div>
                    </div>
                </template>
            </div>
        </van-pull-refresh>

        <div class="save">
            <van-button type="default" size="small" :disabled="active==0" @click="stepHandle('up')">上一步</van-button>
            <van-button type="primary" @click="submitHandle" size="small"
                v-if="!((active==2&&proInfo.status==1)||(active==1&&proTypeList[0].status==1))">保存</van-button>
            <van-button type="default" size="small" :disabled="active==2" @click="stepHandle('down')">下一步</van-button>
        </div>


        <!-- 选择产品类型 -->
        <van-action-sheet v-model="showType" :actions="actions" cancel-text="取消" close-on-click-action
            @cancel="onTypeCancel" @select="selectHandle"></van-action-sheet>
        <!-- 选择公司列表 -->
        <van-action-sheet v-model="showCom" :actions="comList" cancel-text="取消" close-on-click-action
            @cancel="onComCancel" @select="selectComHandle"></van-action-sheet>
        <!-- 选择产品列表 -->
        <van-action-sheet v-model="showProduct" :actions="proAction" cancel-text="取消" close-on-click-action
            @cancel="onComCancel" @select="selectProHandle"></van-action-sheet>



        <!-- 查看pdf -->
        <div class="mask" v-if="pdfDia">
            <van-nav-bar title="预览文件" left-text="关闭" left-arrow @click-left="closeDia"></van-nav-bar>
            <div class="pdfForm">
                <iframe :src="pdfUrl" width="100%" height="100%">
                    This browser does not support PDFs. Please download the PDF to view it: <a :href="pdfUrl">Download
                        PDF</a>
                </iframe>
            </div>
        </div>

        <div class="mask" v-if="mask">
            <img src="../img/load.gif">
        </div>
    </div>


    <script src="../js/vue.min.js"></script>
    <script src="../js/flex_ible/lib-flexible.js"></script>
    <script src="../js/vue-lazyload.js"></script>
    <script src="../js/vant.min.js"></script>
    <script src="../js/vue-lazyload.js"></script>
    <script src="../js/mui.min.js"></script>
    <script src="./js/url.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <script src="../js/ajax.js"></script>
    <script>


        Vue.use(VueLazyload);
        var vm = new Vue({
            el: "#app",
            data: {
                mask: false,
                code: '',
                active: 0,// 步骤条

                // 企业列表
                companyList: [
                    {
                        companyId: '',
                        companyName: '',
                        companyImgList: [],
                        uploadImgList: [],
                        state: ''
                    }
                ],

                // 产品类型
                proTypeList: [
                    {
                        companyId: '',
                        companyName: '',
                        typeId: '',
                        typeName: '',
                        ziZhiList: []
                    }
                ],
                selectIndex: -1,// 所选类型索引
                actions: [],// 产品类型 
                showType: false,// 产品类型弹窗
                showCom: false,// 选择公司弹窗
                // 公司列表
                comList: [{ id: 1, name: '诚赉健康' }, { id: 2, name: '网络科技' }],

                // 产品资料
                proInfo: {
                    companyId: '',
                    companyName: '',
                    typeId: '',
                    typeName: '',
                    productName: '',
                    ziZhiList: []
                },
                allTypeList: [],// 全部分类
                companyInputVal: '',// 文本框公司名

                typeId: '',// 分类id
                ziZhiId: '',// 资质id

                showProduct: false,// 产品列表弹窗
                comProList: [
                    {
                        id: 1, name: '制氧机'
                    },
                    {
                        id: 2, name: '雾化器'
                    }
                ],// 当前公司该分类下的所有商品

                userId: localStorage.recordUserId,
                recordId: '',// 主记录信息

                ziZhiInfo: '',// 当前选择的公司的资质信息·
                isHaveZiZhi: false,// 当前所选公司的这个分类是否已上传过资质

                proList: [],// 当前公司已保存的所有产品
                proAction: [],// 根据类型筛选产品
                showDia: false,//新增产品弹窗
                isLoading: false,
                pdfUrl: 'https://zbz-imgs.oss-cn-beijing.aliyuncs.com/shopInfo/1754101651614442.pdf',
                pdfDia: false,
                isIos: false,
            },
            created() {
                if (localStorage.recordId) {
                    this.recordId = localStorage.recordId
                    console.log(this.recordId);
                    this.getType()
                    this.getData()
                }

                this.isIos = window.navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)

            },
            methods: {
                // 关闭弹窗
                closeDia() {
                    this.pdfDia = false
                },
                showFile(url, type) {
                    if (type == 'pdf') {
                        this.pdfUrl = 'https://zbz-imgs.oss-cn-beijing.aliyuncs.com/qualFile/' + url
                        if (this.isIos) {
                            this.pdfDia = true
                        } else {
                            var a = document.createElement('a');
                            a.href = this.pdfUrl;
                            a.download = '';
                            a.click();
                        }
                    } else if (type == 'doc') {
                        this.pdfUrl = 'https://view.officeapps.live.com/op/embed.aspx?src=https://zbz-imgs.oss-cn-beijing.aliyuncs.com/qualFile/' + url
                        this.pdfDia = true

                    }
                    console.log(type);

                    console.log(url);
                    console.log(this.pdfUrl);

                },
                onRefresh() {
                    this.getType()
                    this.getData()
                    if (this.active == 1) {
                        this.getComZiZhi(this.proTypeList[0].companyId)
                    } else if (this.active == 2) {
                        this.getComZiZhi(this.proInfo.companyId)
                    }
                },


                // 公司名称文本框  失去焦点
                comBlurHandle(index) {
                    if (this.companyList[index].companyId != '' && this.companyInputVal != '' && this.companyList[index].companyName != '' && this.companyList[index].companyName !== this.companyInputVal) {
                        this.$toast('修改了企业名称')
                        console.log(this.companyList[index]);
                        this.editComHandle(this.companyList[index], 1)
                    } else if (this.companyList[index].companyName == '') {
                        console.log(this.companyInputVal);
                        this.companyList[index].companyName = this.companyInputVal
                        this.$toast('请输入企业名称')
                    }
                },

                // 修改已保存的公司信息    flag  1 修改公司名  2 修改公司营业执照图片
                editComHandle(obj, flag) {
                    console.log(obj);
                    console.log(flag);
                    // return
                    var that = this, formData = new FormData();
                    formData.append("companyId", obj.companyId);
                    formData.append("recordId", that.recordId);
                    formData.append("userId", that.userId);

                    if (flag == 1) {
                        formData.append("companyName", obj.companyName);
                    } else if (flag == 2 && obj.companyId != '' && obj.uploadImgList.length != 0) {
                        formData.append("companyFile", obj.uploadImgList[0].file);
                    } else {
                        return
                    }

                    console.log('end');

                    this.mask = true
                    var timer;
                    timer = setInterval(function () {
                        clearInterval(timer);
                        xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                        xhr.open("post", newUrl + "qual/editQualCompany",
                            false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                        xhr.onload = that.uploadComplete; //请求完成
                        xhr.onerror = that.uploadFailed; //请求失败
                        xhr.send(formData); //开始上传，发送form数据
                    }, 1000);
                },

                // 公司名称文本框  获取焦点
                comFocusHandle(index) {
                    if (this.companyList[index].companyId != '') {
                        this.companyInputVal = this.companyList[index].companyName
                        console.log(this.companyInputVal);
                    }
                },


                // 选择公司
                selectComHandle(item) {
                    console.log(item);
                    this.proTypeList[this.selectIndex].companyId = item.id
                    this.proTypeList[this.selectIndex].companyName = item.name
                    this.getComZiZhi(item.id)
                },

                // 选择产品
                selectProHandle(obj) {

                    console.log(obj);

                    if (obj.id == 0) {  // 新添加产品
                        this.proInfo.qualId = ''
                        this.proInfo.productName = ''
                        this.proInfo.status = 0
                        this.proInfo.isHaveZiZhi = false
                        this.proInfo.ziZhiList.forEach(item => {
                            item.imgList = []
                        })
                    } else {
                        var that = this;
                        that.proInfo.ziZhiList.forEach(item => {
                            item.imgList = []
                            obj.imgList.forEach(ele => {
                                if (ele.id == item.proQualId) {
                                    item.imgList.push(ele)
                                }
                            })
                        })
                        this.proInfo.qualId = obj.qualId
                        this.proInfo.productName = obj.name
                        this.proInfo.status = obj.status
                        this.proInfo.feedback = obj.feedback
                        this.proInfo.isHaveZiZhi = true
                        console.log(this.proInfo);


                    }

                    console.log(obj);
                    console.log(this.proInfo);

                },


                // 显示企业弹窗  产品类型弹窗
                showHandle(index, flag) {
                    this.selectIndex = index
                    console.log(flag);
                    console.log(this.selectIndex);

                    if (flag == 'type') {
                        console.log(this.proTypeList[this.selectIndex]);
                        if (this.proTypeList[this.selectIndex].companyId == '') {
                            this.$toast('请选择企业')
                            return
                        }
                        this.showType = true
                    } else if (flag == 'company') {
                        this.showCom = true
                    } else if (flag == 'product') {


                        if (!this.proAction.some(item => {
                            return item.id == 0
                        })) {
                            this.proAction.push({
                                id: 0, name: '添加产品', color: '#999'
                            })
                        }

                        this.showProduct = true
                    }
                },
                // 关闭选择类型弹窗
                onTypeCancel() {
                    this.showType = false
                },
                onComCancel() {
                    this.showCom = false
                },

                // 保存 
                submitHandle() {
                    var formData = new FormData(), names = '', that = this;
                    if (this.active == 0) {  // 保存公司信息
                        var flag = this.companyList.length == 0 || this.companyList.some(item => {
                            return item.companyName == '' || (item.companyId == '' && item.uploadImgList.length == 0) || (item.companyId != '' && item.companyImgList.length == 0)
                        })

                        console.log(this.companyList);
                        if (flag) {
                            this.$toast('请填写完整企业信息')
                            return
                        }

                        this.companyList.forEach(ele => {
                            if (ele.companyId == '') {  // 只有新添加的公司走这个方法
                                names += ele.companyName + ','
                                ele.uploadImgList.forEach(item => {
                                    formData.append("companyFile", item.file)
                                })
                            }
                        })

                        formData.append("recordId", that.recordId);
                        formData.append("userId", that.userId);
                        formData.append("companyNames", names);

                        that.mask = true

                        var timer;
                        timer = setInterval(function () {
                            clearInterval(timer);
                            xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                            xhr.open("post", newUrl + "qual/addQualCompany",
                                false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                            xhr.onload = that.uploadComplete; //请求完成
                            xhr.onerror = that.uploadFailed; //请求失败
                            xhr.send(formData); //开始上传，发送form数据
                        }, 1000);
                    } else if (this.active == 1) {   // 保存产品类型·


                        if (!this.isHaveZiZhi) {
                            var flag = this.proTypeList.some(item => {
                                return item.companyName == '' || item.typeName == '' || (item.ziZhiList.length != 0 && item.ziZhiList.some(ele => {
                                    return ele.uploadImgList.length == 0
                                }))
                            })
                            console.log(this.proTypeList);
                            if (flag) {
                                this.$toast('请填写完整产品类型信息')
                                return
                            }

                            console.log(this.proTypeList);

                            var obj = this.proTypeList[0]

                            // return
                            obj.ziZhiList.forEach(ele => {
                                ele.uploadImgList.forEach(item => {
                                    console.log(item.file.newFile);

                                    formData.append("comfile", item.file.newFile)
                                })
                            })



                            formData.append("userId", that.userId);
                            formData.append("companyId", obj.companyId);
                            formData.append("companyName", obj.companyName);
                            formData.append("qualProductName", 0);
                            formData.append("qualProductId", obj.typeId);

                            that.mask = true

                            var timer;
                            timer = setInterval(function () {
                                clearInterval(timer);
                                xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                                xhr.open("post", newUrl + "qual/addQual",
                                    false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                                xhr.onload = that.uploadComplete; //请求完成
                                xhr.onerror = that.uploadFailed; //请求失败
                                xhr.send(formData); //开始上传，发送form数据
                            }, 1000);

                        } else {
                            var flag = this.proTypeList.some(item => {
                                return item.ziZhiList.some(ele => {
                                    return ele.uploadImgList.length == 0 && ele.imgList.length == 0
                                })
                            })
                            console.log(this.proTypeList);
                            if (flag) {
                                this.$toast('请填写完整产品类型信息')
                                return
                            }

                            console.log(this.proTypeList);

                            var obj = this.proTypeList[0], imgName = '';

                            // return
                            obj.ziZhiList.forEach(ele => {
                                ele.uploadImgList.forEach(item => {
                                    console.log(item.file.newFile);
                                    formData.append("comfile", item.file.newFile)
                                })
                                ele.imgList.forEach(item => {
                                    imgName += item.url + ','
                                })
                            })

                            console.log(imgName);

                            formData.append("qualId", obj.qualId);
                            formData.append("companyId", obj.companyId);
                            formData.append("companyName", obj.companyName);
                            formData.append("qualProductName", 0);
                            formData.append("qualProductId", obj.typeId);
                            formData.append("qualImgs", imgName);
                            formData.append("status", 0);
                            formData.append("feedback", '');

                            console.log(obj);

                            that.mask = true
                            var timer;
                            timer = setInterval(function () {
                                clearInterval(timer);
                                xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                                xhr.open("post", newUrl + "qual/editQual",
                                    false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                                xhr.onload = that.uploadComplete; //请求完成
                                xhr.onerror = that.uploadFailed; //请求失败
                                xhr.send(formData); //开始上传，发送form数据
                            }, 1000);

                        }
                    } else if (this.active == 2) {

                        console.log(this.proInfo);
                        // return


                        if (!this.proInfo.isHaveZiZhi) {
                            if (this.proInfo.productName == '') {
                                this.$toast('请输入产品名称')

                                return
                            }

                            if (this.proInfo.ziZhiList.some(item => {
                                return item.uploadImgList.length == 0
                            })) {
                                this.$toast('请上传资质图片')
                                return
                            }

                            var obj = this.proInfo
                            obj.ziZhiList.forEach(ele => {
                                ele.uploadImgList.forEach(item => {
                                    console.log(item.file.newFile);
                                    formData.append("profile", item.file.newFile)
                                })
                            })

                            formData.append("userId", that.userId);
                            formData.append("companyId", obj.companyId);
                            formData.append("companyName", obj.companyName);
                            formData.append("qualProductName", obj.productName);
                            formData.append("qualProductId", obj.typeId);

                            that.mask = true

                            var timer;
                            timer = setInterval(function () {
                                clearInterval(timer);
                                xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                                xhr.open("post", newUrl + "qual/addQual",
                                    false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                                xhr.onload = that.uploadComplete; //请求完成
                                xhr.onerror = that.uploadFailed; //请求失败
                                xhr.send(formData); //开始上传，发送form数据
                            }, 1000);

                        } else {
                            var obj = this.proInfo, imgName = '';
                            if (this.proInfo.ziZhiList.some(item => {
                                return item.uploadImgList.length == 0 && item.imgList.length == 0
                            })) {
                                this.$toast('请上传资质图片')
                                return
                            }
                            // return
                            obj.ziZhiList.forEach(ele => {
                                ele.uploadImgList.forEach(item => {
                                    console.log(item.file.newFile);
                                    formData.append("profile", item.file.newFile)
                                })
                                ele.imgList.forEach(item => {
                                    imgName += item.url + ','
                                })
                            })


                            console.log(imgName);

                            formData.append("qualId", obj.qualId);
                            formData.append("companyId", obj.companyId);
                            formData.append("companyName", obj.companyName);
                            formData.append("qualProductName", obj.productName);
                            formData.append("qualProductId", obj.typeId);
                            formData.append("qualImgs", imgName);

                            formData.append("status", 0);
                            formData.append("feedback", '');

                            that.mask = true

                            var timer;
                            timer = setInterval(function () {
                                clearInterval(timer);
                                xhr = new XMLHttpRequest(); // XMLHttpRequest 对象
                                xhr.open("post", newUrl + "qual/editQual",
                                    false); //post方式，url为服务器请求地址，true 该参数规定请求是否异步处理。
                                xhr.onload = that.uploadComplete; //请求完成
                                xhr.onerror = that.uploadFailed; //请求失败
                                xhr.send(formData); //开始上传，发送form数据
                            }, 1000);

                        }


                    }
                },

                // 公司信息 上传成功 
                uploadComplete: function (evt) {
                    //服务断接收完文件返回的结果
                    var that = this
                    that.mask = false;
                    var data = JSON.parse(evt.target.responseText);
                    console.log(data);

                    if (data.flag) {
                        if (that.active == 0) {
                            that.getData()
                        } else if (that.active == 1) {
                            that.getComZiZhi(that.proTypeList[0].companyId)
                        } else if (that.active == 2) {
                            // that.getData()
                            that.getComZiZhi(that.proInfo.companyId)
                        }

                        that.mask = false;
                        that.$toast({
                            position: 'bottom',
                            message: '保存成功'
                        });
                    } else {
                        that.$toast({
                            position: 'bottom',
                            message: '保存失败'
                        });
                    }
                },
                // 公司信息 上传失败
                uploadFailed: function (evt) {
                    this.mask = false;
                    this.$toast({
                        position: 'bottom',
                        message: '保存失败，请重试'
                    });
                },
                // 添加公司
                addCom() {
                    this.companyList.push({
                        companyId: '',
                        companyName: '',
                        companyImgList: [],
                        uploadImgList: [],
                        state: ''
                    })
                },
                // 移除公司项
                removeCom(index) {


                    if (this.companyList.length == 1) {
                        this.$toast('请至少保留一个企业')
                        return
                    }
                    this.companyList.splice(index, 1)
                    console.log(this.companyList);

                },

                // 上一步  下一步
                stepHandle(flag) {
                    if (flag == 'up') {
                        this.active -= 1
                    } else {
                        console.log(this.active);
                        console.log(this.selectIndex);
                        console.log(this.proTypeList[this.selectIndex]);

                        if (this.active == 0 && this.companyList.length == 0) {
                            this.$toast('请添加企业')
                            return
                        } else if (this.active == 1 && (this.selectIndex == -1 || this.proTypeList[this.selectIndex].companyId == '')) {
                            this.$toast('请选择企业')
                            return
                        } else if (this.active == 1 && this.proTypeList[this.selectIndex].ziZhiList.some(item => {
                            return item.imgList.length == 0
                        })) {
                            this.$toast('请先完善相关资质')
                            return
                        } else if (this.active == 1 && this.proInfo.typeId == '') {
                            this.$toast('请选择产品类型')
                            return
                        } else {
                            this.active += 1
                        }
                    }
                },
                // 返回
                onClickLeft() {
                    window.location.href = './step.html'
                },



                // 获取该记录的公司列表
                getData() {
                    var that = this;
                    this.mask = true
                    ajax({
                        url: newUrl + "qual/getQualCompanyListByRecordId",
                        type: 'post',
                        data: {
                            recordId: that.recordId
                        },
                        dataType: 'json',
                        timeout: 10000,
                        contentType: "application/json",
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(res);
                            that.mask = false
                            that.isLoading = false
                            if (res.flag) {
                                // qualFile
                                res.data.reverse()
                                that.companyList = []
                                that.comList = []

                                res.data.forEach(item => {
                                    that.comList.push({
                                        id: item.companyId,
                                        name: item.companyName
                                    })
                                    that.companyList.push({
                                        companyId: item.companyId,
                                        companyName: item.companyName,
                                        companyImgList: item.busLicence.split(' '),
                                        uploadImgList: [],
                                        state: item.state
                                    })
                                })
                                console.log(that.companyList);
                                console.log(res.data);
                            }
                        },
                    })
                },
                // /qual/getQual   获取公司的相关资质
                getComZiZhi(companyId) {
                    var that = this;
                    this.mask = true
                    ajax({
                        url: newUrl + "qual/getQual",
                        type: 'post',
                        data: {
                            companyId
                        },
                        dataType: 'json',
                        timeout: 10000,
                        contentType: "application/json",
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(res);
                            that.mask = false
                            that.isLoading = false
                            if (res.flag) {
                                console.log(res.data);
                                that.ziZhiInfo = res.data
                                that.proList = []
                                res.data.qualMaterials.forEach(item => {
                                    item.imgList = []
                                    item.id = item.qualId
                                    item.name = item.qualProductName
                                    console.log();

                                    if (Object.keys(item.comMap).length != 0) {
                                        for (const key in item.comMap) {
                                            item.comMap[key].split(',').forEach(ele => {
                                                ele = ele.trim()
                                                var str = ele.split('.')[0].split('%')[0] + '.' + ele.split('.')[1]
                                                console.log(str);

                                                var type = '';
                                                console.log(str.indexOf('pdf') != -1);

                                                if (str.indexOf('pdf') != -1) {
                                                    type = 'pdf'
                                                } else if (str.indexOf('doc') != -1) {
                                                    type = 'doc'
                                                } else {
                                                    type = 'img'
                                                }
                                                item.imgList.push({
                                                    id: key,
                                                    img: str.trim(),
                                                    url: ele,
                                                    type
                                                })
                                            })
                                        }
                                    } else if (Object.keys(item.proMap).length != 0) {
                                        for (const key in item.proMap) {
                                            item.proMap[key].split(',').forEach(ele => {
                                                ele = ele.trim()
                                                var str = ele.split('.')[0].split('%')[0] + '.' + ele.split('.')[1]
                                                console.log(str);

                                                var type = '';
                                                console.log(str.indexOf('pdf') != -1);

                                                if (str.indexOf('pdf') != -1) {
                                                    type = 'pdf'
                                                } else if (str.indexOf('doc') != -1) {
                                                    type = 'doc'
                                                } else {
                                                    type = 'img'
                                                }
                                                item.imgList.push({
                                                    id: key,
                                                    img: str.trim(),
                                                    url: ele,
                                                    type
                                                })
                                            })
                                        }
                                    }

                                    if (item.qualProductName != null && item.qualProductName != '0') {
                                        that.proList.push(item)
                                    }
                                })

                                if (that.active == 1 && that.proTypeList[that.selectIndex].typeId != '') {
                                    var obj = {
                                        id: that.proTypeList[that.selectIndex].typeId,
                                        name: that.proTypeList[that.selectIndex].typeName,
                                    }
                                    that.selectHandle(obj)
                                }


                                that.proAction = JSON.parse(JSON.stringify(that.proList))

                                if (that.active == 2) {
                                    that.proAction = that.proList.filter(item => {
                                        return item.qualProductId == that.proInfo.typeId
                                    })
                                    var obj = {
                                        id: that.proTypeList[that.selectIndex].typeId,
                                        name: that.proTypeList[that.selectIndex].typeName,
                                    }
                                    that.selectHandle(obj)
                                }
                                console.log(res);
                                console.log(that.proList);

                            }
                        },
                    })
                },



                // 重命名文件
                resName(file, tId, zId) {
                    const tempFile = file
                    console.log(tempFile);

                    var arr = [];
                    arr = tempFile.name.split('.')
                    console.log(arr);
                    console.log(arr[arr.length - 1]);
                    const type = tempFile.type
                    console.log(type);
                    const size = tempFile.size
                    // const newName = arr[0] + '%' + tId + '%' + zId + '.' + arr[arr.length - 1]
                    const newName = arr[0] + '%' + zId + '.' + arr[arr.length - 1]
                    let uploadFile = new File([tempFile], newName, { type, size })
                    return uploadFile
                },
                beforeRead(file) {
                    console.log(file);


                    // if ()
                    console.log(Array.isArray(file));
                    // console.log(file.length);


                    if (Array.isArray(file)) {
                        file.forEach(ele => {
                            ele.newFile = this.resName(ele, this.typeId, this.ziZhiId)
                        });
                    } else {
                        file.newFile = this.resName(file, this.typeId, this.ziZhiId)
                    }



                    console.log(this.typeId);
                    console.log(this.ziZhiId)


                    // file.newFile = this.resName(file, this.typeId, this.ziZhiId)

                    return true

                },


                getType() {
                    var that = this;
                    this.mask = true
                    ajax({
                        url: newUrl + "qual/getQualProduct",
                        type: 'post',
                        data: {},
                        dataType: 'json',
                        timeout: 10000,
                        contentType: "application/json",
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(res);
                            that.isLoading = false
                            if (res.flag) {
                                that.actions = []
                                res.data.forEach(ele => {
                                    that.actions.push({
                                        id: ele.qualProductId,
                                        name: ele.productName
                                    })
                                })
                                that.allTypeList = JSON.parse(JSON.stringify(that.actions))
                                console.log(that.actions);
                                that.mask = false
                            }
                        },
                    })
                },



                // 选择产品类型
                selectHandle(item) {
                    console.log(item);
                    var that = this
                    that.mask = true
                    ajax({
                        url: newUrl + "qual/getQualByProductId",
                        type: 'post',
                        data: { id: item.id },
                        dataType: 'json',
                        timeout: 10000,
                        contentType: "application/json",
                        success: function (res) {
                            var res = JSON.parse(res)
                            console.log(res);
                            that.mask = false

                            if (res.flag) {
                                console.log(res.data);



                                console.log(that.ziZhiInfo.qualMaterials);


                                var arr = that.ziZhiInfo.qualMaterials.filter(ele => {
                                    return ele.qualProductId == item.id && Object.keys(ele.proMap).length == 0
                                })
                                // arr[0].imgList
                                console.log(arr);

                                // 是否已上传资质
                                that.isHaveZiZhi = arr.length != 0

                                res.data.qualComCategories.forEach(cItem => {
                                    cItem.uploadImgList = []
                                    cItem.imgList = []
                                    cItem.status = -1
                                    cItem.passNote = ''

                                    if (arr.length != 0) {
                                        arr[0].imgList.forEach(ele => {
                                            if (ele.id == cItem.comQualId) {
                                                cItem.imgList.push(ele)
                                            }
                                        })
                                    }
                                })

                                if (arr.length != 0) {
                                    that.proTypeList[that.selectIndex].status = arr[0].status
                                    that.proTypeList[that.selectIndex].feedback = arr[0].feedback
                                } else {
                                    that.proTypeList[that.selectIndex].status = 0
                                }
                                that.proTypeList[that.selectIndex].ziZhiList = res.data.qualComCategories
                                that.proTypeList[that.selectIndex].typeId = item.id
                                that.proTypeList[that.selectIndex].typeName = item.name
                                that.proTypeList[that.selectIndex].qualId = that.isHaveZiZhi ? arr[0].qualId : 0
                                console.log(that.proTypeList);


                                that.proInfo = {
                                    companyId: that.proTypeList[that.selectIndex].companyId,
                                    companyName: that.proTypeList[that.selectIndex].companyName,
                                    typeId: item.id,
                                    typeName: item.name,
                                    productName: '',
                                    ziZhiList: [],
                                    isHaveZiZhi: false
                                }

                                res.data.qualProCategories.forEach(pItem => {
                                    pItem.uploadImgList = []
                                    pItem.imgList = []
                                    pItem.status = -1
                                    pItem.passNote = ''
                                })
                                that.proInfo.ziZhiList = res.data.qualProCategories
                                console.log(that.proInfo);

                                that.proAction = that.proList.filter(item => {
                                    return item.qualProductId == that.proInfo.typeId
                                })

                                // 判断当前分类下是否有产品
                                // that.proInfo.isHaveZiZhi = that.proAction.length != 0

                                console.log(that.proInfo);
                                that.onTypeCancel()



                            }
                        },
                    })


                },

                checkImg(list, flag, index) {
                    console.log(list);
                    console.log(path);
                    console.log(index);

                    var arr = [], str = '';
                    list.forEach(ele => {
                        if (flag == 1) {
                            str = path + 'qualFile/' + ele
                        } else if (flag == 2) {
                            str = path + 'qualFile/' + ele.img
                        }
                        arr.push(str)
                    })
                    console.log(arr);
                    vant.ImagePreview({
                        images: arr,
                        startPosition: index
                    });
                },
                delImg(list, index, imgIndex) {
                    console.log(list[index].imgList[imgIndex]);
                    // list[index].delImg.push(list[index].imgList[imgIndex])
                    list[index].imgList.splice(imgIndex, 1)
                    console.log(list);
                },


                // 删除公司营业执照
                delComImg(list, index, imgIndex) {
                    console.log(list[index].companyImgList[imgIndex]);
                    list[index].companyImgList = []
                    this.$toast('请上传新的营业执照')
                },


            }
        })



    </script>

</body>

</html>